<?php


namespace app\index\controller;


use app\BaseController;
use app\index\validate\uploadRule;
use think\exception\ValidateException;

class Image extends BaseController
{
    public function upload(){
        if(!$this->request->isPost()){
            return show(config('status.error'),'上传方式不合法');
        }
        $file = $this->request->file('files');
        $filename = \think\facade\Filesystem::disk('public')->putFile("upload/image",$file);
        if(!$filename){
            return show(config('status.error'),'上传图片失败');
        }
        $imageUrl = '/storage/'.$filename;
        return show(config('status.success'),'上传图片成功',$imageUrl);
    }

    public function uploadWord(){
        $file = $this->request->file('files');
        try {
            validate(uploadRule::class)->scene('accessory')->check(['accessory'=>$file]);
        }catch (ValidateException $e){
            return show(config('status.error'),$e->getError());
        }
        $filename = \think\facade\Filesystem::disk('public')->putFile("upload/word",$file);
        if(!$filename){
            return show(config('status.error'),'上传图片失败');
        }
        $imageUrl = '/storage/'.$filename;
        return show(config('status.success'),'上传图片成功',$imageUrl);
    }
    public function removeFile(){
//        $filename = "../public/storage/upload/word/20210402\d94dc591a8649f64e7ec5e8486e85e71.doc";
        $url = $this->request->param("file");

        try {
            $filename = "../public".$url;
            $filename = unlink($filename);
        } catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        if($filename){
            return show(config('status.success'),'删除成功');
        }else{
            return show(config('status.success'),'删除失败');
        }
    }
}